package main

import (
	"github.com/gogf/gf/encoding/gjson"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/os/gfile"
	"github.com/gogf/gf/os/glog"
	"github.com/zhang201702/zhang/z"
	"github.com/zhang201702/zhang/zfile"
	"github.com/zhang201702/zhang/zlog"
	"os/exec"
	"runtime"
	"time"
	"usad2/app/api"

	"github.com/zhang201702/zhang"
	"github.com/zhang201702/zhang/zconfig"
)

func main() {
	a := getDefaultConfigPath()
	zlog.Log(a)
	port := zconfig.Conf.GetString("port")
	uTime := time.Now().Unix()
	openBrowse("http://localhost:" + port + "?t=" + z.String(uTime))
	s := zhang.Default()

	api.Register(s)

	reportAip := api.ReportApi{}
	reportAip.Register(s)

	excelData := map[string]interface{}(nil)
	s.BindHandler("/excel/data/upload", func(r *ghttp.Request) {
		s := r.GetRawString()
		gj := gjson.New(s)
		excelData = gj.ToMap()
		r.Response.Write("ok")
	})
	s.BindHandler("/excel/data", func(r *ghttp.Request) {
		r.Response.WriteJson(excelData)
	})
	//apiFont := api.FontApi{}
	//apiFont.Register(s)
	//
	//apiCert := api.CertApi{}
	//apiCert.Register(s)

	//apiCertTxet := api.NewApi("cert_text", "certText")
	//apiCertTxet.Register(s)

	//apiPpt := api.PptApi{}
	//apiPpt.Register(s)

	glog.SetDebug(zconfig.Debug)
	s.SetDumpRouteMap(zconfig.Debug)

	s.Run()
}

func openBrowse(url string) {
	sysType := runtime.GOOS
	if sysType == "windows" {
		exec.Command(`cmd`, `/c`, `start`, url).Start()

	} else {
		exec.Command(`open`, url).Start()
	}
}

func getDefaultConfigPath() string {

	path, _ := gfile.Search("config.json")
	zlog.Log("1", path)
	if path != "" {
		return path
	}
	path, _ = gfile.Search("config/config.json")
	zlog.Log("2", path)
	if zfile.PathExists(path) {
		return path
	}
	path, _ = gfile.Search("")
	zlog.Log("3", path)
	return ""
}
